package com.servlet;

import com.pojo.Admins_Accounts;
import com.service.Admins_AccountsService;
import com.service.impl.Admins_AccountsServiceImpl;
import com.util.PageUtil;
import com.alibaba.fastjson.JSON;
import com.util.SmsUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "Admins_AccountsServlet", value = "/Admins_AccountsServlet")
public class Admins_AccountsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String tag=request.getParameter("tag");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        if(tag.equals("getData")){
            Admins_AccountsService adminService=new Admins_AccountsServiceImpl();
            String query=new String(request.getParameter("query").getBytes("iso-8859-1"),"utf-8");
            int pageIndex=Integer.parseInt(request.getParameter("currentPage"));
            int pageSize=Integer.parseInt(request.getParameter("pagesize"));
            int pageTotal=0;
            if(adminService.count(query)%pageSize!=0){
                pageTotal=adminService.count(query)/pageSize+1;
            }else {
                pageTotal=adminService.count(query)/pageSize;
            }
            PageUtil<Admins_Accounts> pageUtil=new PageUtil<>(
                    pageIndex,
                    pageSize,
                    adminService.count(query),
                    pageTotal,
                    adminService.pages(pageIndex,pageSize,query)
                    );
            response.getWriter().print(JSON.toJSONString(pageUtil));
        } else if (tag.equals("Del")) {
            Admins_AccountsService service = new Admins_AccountsServiceImpl();
            int id = Integer.parseInt(request.getParameter("id"));
            if (service.Delete(id) > 0) {
                response.getWriter().print(true);
            } else {
                response.getWriter().print(false);
            }
        } else if (tag.equals("GetById")) {
            Admins_AccountsService service = new Admins_AccountsServiceImpl();
            int id = Integer.parseInt(request.getParameter("id"));
            Admins_Accounts admins_accounts = service.GetById(id);
            response.getWriter().print(JSON.toJSONString(admins_accounts));
        } else if (tag.equals("getUser")) {
            HttpSession session = request.getSession();
            Admins_AccountsService service = new Admins_AccountsServiceImpl();
            String username = session.getAttribute("USER_NAME").toString();
            int id =service.getUidByName(username);
            Object obj=new Object[]{id, username};
            response.getWriter().print(JSON.toJSONString(obj));
        }else if(tag.equals("SendCode")){
            String phone=request.getParameter("phone");
            HttpSession session=request.getSession();
            response.getWriter().print(SmsUtil.send(phone,session));
        } else if (tag.equals("Logout")) {
            HttpSession session=request.getSession();
            session.invalidate();
            response.getWriter().print(true);
        } else if (tag.equals("getUserRole")) {
            int uid=Integer.parseInt(request.getParameter("uid"));
            Admins_AccountsService service=new Admins_AccountsServiceImpl();
            response.getWriter().print(service.getRole(uid));
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String tag=request.getParameter("tag");
        if(tag.equals("Add")){
            String name=request.getParameter("name");
            String password=request.getParameter("password");
            String phone=request.getParameter("phone");
            String email=request.getParameter("email");
            String role=request.getParameter("role");
            String username=request.getParameter("username");
            Admins_Accounts admins_accounts=new Admins_Accounts(username,password,name,email,role,phone);
            Admins_AccountsService Service=new Admins_AccountsServiceImpl();
            if(Service.Add(admins_accounts)>0){
                response.getWriter().print(true);
            }else {
                response.getWriter().print(false);
            }
        } else if (tag.equals("Update")) {
            String name=request.getParameter("name");
            String password=request.getParameter("password");
            String phone=request.getParameter("phone");
            String email=request.getParameter("email");
            String role=request.getParameter("role");
            String username=request.getParameter("username");
            int id=Integer.parseInt(request.getParameter("id"));
            Admins_Accounts admins_accounts=new Admins_Accounts(id,username,password,name,email,role,phone);
            Admins_AccountsService Service=new Admins_AccountsServiceImpl();
            if(Service.Update(admins_accounts)>0){
                response.getWriter().print(true);
            }else {
                response.getWriter().print(false);
            }
        } else if (tag.equals("Login")) {
            HttpSession session=request.getSession();
            String username=request.getParameter("account");
            String password=request.getParameter("pwd");
            Admins_AccountsService service=new Admins_AccountsServiceImpl();
            if(service.Login(username,password)){
                if(!session.isNew()){
                    session.setAttribute("USER_NAME",username);
                    session.setMaxInactiveInterval(2*60*60);
                }
                response.getWriter().print(true);
            }else {
                response.getWriter().print(false);
            }
        } else if (tag.equals("Checkcode")) {
            HttpSession session=request.getSession();
            String checkcode=session.getAttribute("checkcode").toString();
            String code=request.getParameter("code");
            if(!checkcode.equals(code)){
                response.getWriter().print(false);
            }else {
                response.getWriter().print(true);
            }
        } else if (tag.equals("LoginByPhone")) {
            String sessionCode=request.getSession().getAttribute("phoneCode").toString();
            String code=request.getParameter("code");
            HttpSession session=request.getSession();
            if(sessionCode.equals(code)){
                String username=session.getAttribute("userNameTemp").toString();
                session.setAttribute("USER_NAME",username);
                session.setMaxInactiveInterval(2*60*60);
                response.getWriter().print(true);
            }else {
                response.getWriter().print(false);
            }
        } else if (tag.equals("CheckPhone")) {
            Admins_AccountsService service=new Admins_AccountsServiceImpl();
            String username=service.getUnameByPhone(request.getParameter("phone"));
            if(username!=null){
                HttpSession session=request.getSession();
                if(!session.isNew()){
                    session.setAttribute("userNameTemp",username);
                }
                response.getWriter().print(true);
            }else {
                response.getWriter().print(false);
            }
        }else if (tag.equals("CheckUname")) {
            Admins_AccountsService service=new Admins_AccountsServiceImpl();
            String username=request.getParameter("username");
            response.getWriter().print(service.CheckUname(username));
        } else if (tag.equals("CheckPhone2")) {
            Admins_AccountsService service=new Admins_AccountsServiceImpl();
            String phone=request.getParameter("phone");
            response.getWriter().print(service.CheckPhone(phone));
        }
    }
}
 
